package cn.tedu.orderservice.repository.impl;

import cn.tedu.orderservice.constant.OrderStatusConstant;
import cn.tedu.orderservice.mapper.OrderMapper;
import cn.tedu.orderservice.pojo.dto.DeviceChargingDTO;
import cn.tedu.orderservice.pojo.po.ChargingBillFailPO;
import cn.tedu.orderservice.pojo.po.ChargingBillSuccessPO;
import cn.tedu.orderservice.repository.OrderRepository;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Slf4j
@Repository
public class OrderRepositoryImpl implements OrderRepository {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void saveSuccessful(DeviceChargingDTO deviceChargingDTO) {
        log.debug("R1-成功订单参数:{}",deviceChargingDTO);
        ChargingBillSuccessPO chargingBillSuccessPO =new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillId(deviceChargingDTO.getOrderNo());
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_PROGRESS);
        chargingBillSuccessPO.setUserId(deviceChargingDTO.getUserId());
        log.debug("R1-1新增成功数据:{}",chargingBillSuccessPO);
        //mybatis-plus通过@TableId设置了主键,不需要在传id了
        //orderMapper.insert(chargingBillSuccess);

        //mybatis方式,引入了mybatis-plus依赖,为mybatis配置的xml路径会失效
        //mybatis-plus是mybatis的增强,建议去掉mybatis依赖
        orderMapper.saveSuccessful(chargingBillSuccessPO);

    }

    @Override
    public void saveFailed(DeviceChargingDTO deviceChargingDTO) {
        log.debug("R2-失败订单参数:{}",deviceChargingDTO);
        ChargingBillFailPO chargingBillFailPO=new ChargingBillFailPO();
        chargingBillFailPO.setBillId(deviceChargingDTO.getOrderNo());
        chargingBillFailPO.setFailDesc("设备自检失败");
        log.debug("R2-1-新增失败订单:{}",chargingBillFailPO);
        orderMapper.saveFailed(chargingBillFailPO);

    }

    /**
     * 将正常充电的订单更改为异常
     * @param orderNo
     */
    @Override
    public void updateStatusFromNormalToException(String orderNo) {
        log.debug("R3-更新异常订单:{}",orderNo);
        ChargingBillSuccessPO chargingBillSuccessPO=new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillId(orderNo);
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_EXCEPTION_END);
        orderMapper.updateStatusNormalToException(chargingBillSuccessPO);
    }

    /**
     * 获取成功订单
     * @param billId
     * @return
     */
    @Override
    public ChargingBillSuccessPO getSuccess(String billId) {
        log.debug("R-4查询成功订单:{}",billId);
        return orderMapper.getSuccess(billId);
    }

    /**
     * 获取失败订单
     * @param billId
     * @return
     */
    @Override
    public ChargingBillFailPO getFail(String billId) {
        log.debug("R-5查询失败订单:{}",billId);
        return orderMapper.getFail(billId);
    }

    /**
     * 更新成功订单
     * @param orderNo
     * @param gunId
     */
    @Override
    public void updateSuccessOrder(String orderNo, Long gunId) {
        log.debug("R-6补充成功订单信息:{}",gunId);
        orderMapper.updateSuccessOrder(orderNo,gunId);
    }

    /**
     * 更新订单状态到正常结束
     * @param orderNo
     */
    @Override
    public void updateStatus2NormalStop(String orderNo) {
        log.debug("R7-更新用户手动结束订单:{}",orderNo);
        ChargingBillSuccessPO chargingBillSuccessPO=new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillId(orderNo);
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_NORMAL_END);
        orderMapper.updateStatus2NormalStop(chargingBillSuccessPO);
    }
}
